﻿using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.TaskManager.Data;

namespace DotNetNuke.Modules.TaskManager.Components
{
    public class TaskController
    {
        public static Task GetTask(int taskId)
        {
            return CBO.FillObject<Task>(DataProvider.Instance().GetTask(taskId));
        }

        public static List<Task> GetTasks(int moduleId)
        {
            return CBO.FillCollection<Task>(DataProvider.Instance().GetTasks(moduleId));
            
        }

        public static void DeleteTask(int taskId)
        {
            DataProvider.Instance().DeleteTask(taskId);
        }

        public static void DeleteTasks(int moduleId)
        {
            DataProvider.Instance().DeleteTasks(moduleId);
        }

        public static int SaveTask(Task t, int tabId)
        {
             // set up listener            
            string filename = @"C:\Users\Jeff McVey\Desktop\TaskController.txt";
            FileStream traceLog = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);           
            TextWriterTraceListener listener = new TextWriterTraceListener(traceLog);            
            listener.WriteLine("TaskController File Opened *****************");
            
            top:                

            if(t.TaskId<1)
            {
                          
                // output to listener 
                listener.WriteLine("TC t.ContentItemId 1: " + t.ContentItemId.ToString());
              
                t.TaskId = DataProvider.Instance().AddTask(t);
                
                //add content item integration
                var cntTaxonomy = new Taxonomy.Content();
                var objContentItem = cntTaxonomy.CreateContentItem(t, tabId);
                t.ContentItemId = objContentItem.ContentItemId;

               
                // output to listener 
                listener.WriteLine("TC t.ContentItemId 2: " + t.ContentItemId.ToString());
                
                
               // SaveTask(t, tabId);

                goto top;

            }
            else
            {
                // output to listener 
                listener.WriteLine("TC t.ContentItemId 2a: " + t.ContentItemId.ToString());


            //    Commented out to see what happens. 
            //    
            //    No, let's not comment it out just yet.
            //  
                  DataProvider.Instance().UpDateTask(t);

              
                // output to listener 
                listener.WriteLine("TC t.ContentItemId 3: " + t.ContentItemId.ToString());
                
                var cntTaxonomy = new Taxonomy.Content();
                cntTaxonomy.UpdateContentItem(t,tabId);

                // output to listener 
                listener.WriteLine("TC t.ContentItemId 4: " + t.ContentItemId.ToString());
                
             }

           
            // flush any open output before termination
            //   maybe in an override of Form.OnClosed 
            listener.Flush();
            listener.Close();

           
            return t.TaskId;
        }
    }
}








